package crawler.utility.file;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class FileOperation {
	
	public static final String LINE_SEPARATOR = System.getProperty("line.separator");
	
	private FileInputStream fis = null;
	private InputStreamReader isr = null;
	private BufferedReader br = null;
	private FileOutputStream fos = null;
	private PrintWriter pw = null;
	
	private File file = null;
	
	/**
	 * 
	 * 默认路径存在，则传入路径加文件名：F:\pic\test.jpg
	 * 
	 * @param filePath
	 */
	public FileOperation(String filePath){
		file = new File(filePath);
	}
	
	/**
	 * 默认路径不一定存在，则传入路径和文件名
	 * 注意：路径最后不带斜杠
	 * 
	 * @param dirPath    F:\pic 
	 * @param filePath   test.jpg
	 */
	public FileOperation(String dirPath,String filePath){
		File dir = new File(dirPath);
		if (!dir.exists()) {
			dir.mkdirs();
		}
		
		file = new File(dirPath + "/" +filePath);
	}
	
	public void createContentFile() throws IOException {
		createContentFile(null);
	}
	
	public void createContentFile(String filein) throws IOException {
		
		StringBuffer buffer = new StringBuffer();
		
		boolean isFile;
		try {
			isFile = file.createNewFile();
			
			if (!isFile) {
				throw new IOException("File Exists");
			}
			
			if (filein != null) {
				
				fis = new FileInputStream(file);
				isr = new InputStreamReader(fis);
				br = new BufferedReader(isr);
				
				buffer.append(filein);

				fos = new FileOutputStream(file);
				pw = new PrintWriter(fos);
				pw.write(buffer.toString().toCharArray());
				pw.flush();
			}
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
			throw new IOException(e);
		} finally {
			if (pw != null) {
				pw.close();
			}
			try {
				if (fos != null) {
					fos.close();
				}
				if (br != null) {
					br.close();
				}
				if (isr != null) {
					isr.close();
				}
				if (fis != null) {
					fis.close();
				}
			} catch (IOException e2) {
			}

		}
	}
	
	
	public void createBinaryFile(InputStream inputStream) throws IOException {

		boolean isFile;
		try {
			isFile = file.createNewFile();

			if (!isFile) {
				throw new IOException("File Exists");
			}

			if (inputStream != null) {

				fos = new FileOutputStream(file);

				byte[] buf = new byte[1024];
				int len = 0;
				while ((len = inputStream.read(buf)) != -1) {
					fos.write(buf, 0, len);
				}
				
				fos.flush();
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
			throw new IOException(e);
		} finally {
			if (pw != null) {
				pw.close();
			}
			try {
				if (fos != null) {
					fos.close();
				}
				if (br != null) {
					br.close();
				}
				if (isr != null) {
					isr.close();
				}
				if (fis != null) {
					fis.close();
				}
			} catch (IOException e2) {
			}

		}

	}
	
	public InputStream readBinaryFile() throws IOException {
		if (!exists()) {
			throw new IOException("File not Exists");
		}
		
		InputStream inputStream = null;
		try {
	
			inputStream = new FileInputStream(file);
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			if (pw != null) {
				pw.close();
			}
			try {
				if (fos != null) {
					fos.close();
				}
				if (br != null) {
					br.close();
				}
				if (isr != null) {
					isr.close();
				}
				if (fis != null) {
					fis.close();
				}
			} catch (IOException e2) {
			}

		}
		
		return inputStream;
	}
	

	public String readContentFile() throws IOException {
		
		if (!exists()) {
			throw new IOException("File not Exists");
		}
		
		StringBuffer buffer = new StringBuffer();
		String temp = "";
		
		try {
			fis = new FileInputStream(file);
			isr = new InputStreamReader(fis);
			br = new BufferedReader(isr);

			// 文件原有内容
			while ((temp = br.readLine()) != null) {
				buffer.append(temp);
				// 行与行之间的分隔符 相当于“\n”
				buffer = buffer.append(LINE_SEPARATOR);
			}
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (pw != null) {
				pw.close();
			}
			try {
				if (fos != null) {
					fos.close();
				}
				if (br != null) {
					br.close();
				}
				if (isr != null) {
					isr.close();
				}
				if (fis != null) {
					fis.close();
				}
			} catch (IOException e2) {
			}

		}


		return buffer.toString();
	}
	
	
	public void writeContentFile(String filein) throws IOException{
		writeContentFile(filein, true);
	}
	
	public void writeContentFile(String filein,boolean isNewline) throws IOException {
		
		if (!exists()) {
			throw new IOException("File not Exists");
		}
		
		StringBuffer buffer = new StringBuffer();
		String temp = "";
		
		try {
			fis = new FileInputStream(file);
			isr = new InputStreamReader(fis);
			br = new BufferedReader(isr);

			// 文件原有内容
			while ((temp = br.readLine()) != null) {
				buffer.append(temp);
				// 行与行之间的分隔符 
				if (isNewline) {
					buffer = buffer.append(LINE_SEPARATOR);
				}
			}
			
			buffer = buffer.append(filein);

			fos = new FileOutputStream(file);
			pw = new PrintWriter(fos);
			pw.write(buffer.toString().toCharArray());
			pw.flush();

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (pw != null) {
				pw.close();
			}
			try {
				if (fos != null) {
					fos.close();
				}
				if (br != null) {
					br.close();
				}
				if (isr != null) {
					isr.close();
				}
				if (fis != null) {
					fis.close();
				}
			} catch (IOException e2) {
			}

		}

	}

	public boolean exists() {
		return file.exists();
	}
}
